
#ifndef __BBOX_H__
#define __BBOX_H__

#include "Real3.h"
#include "TEMath.h"

#define MIN(x, y) (x < y ? x : y)
#define MAX(x, y) (x > y ? x : y)

struct D3DXMATRIX;

class BBox
{
public:

	// Constructor
	BBox();
	BBox(const Real3 &_point, const Vector3 &_extent);
	BBox(const BBox &box, const D3DXMATRIX *transform);
	BBox(const Real3 &a, const Real3 &b, const Real3 &c);

	// Operators
	BBox friend operator + (const BBox &a, const BBox &b);
	BBox operator += (const BBox &a);

	BBox friend operator + (const BBox &a, const Real3 &b);
	BBox friend operator - (const BBox &a, const Real3 &b);
	BBox operator += (const Real3 &a);
	BBox operator -= (const Real3 &a);

	// Checks to see if the BBox is set
	bool IsGood();

	// Adds a point to the bounding box
	void AddPoint(const Real3 &p);

	// Returns boundries
	Real3 Min() const { return point; }
	Real3 Max() const { return point + extent; }
	Real3 Extent() const { return extent; }

	// Returns the volume
	float Volume() const { return extent.x * extent.y * extent.z; }

	// Center point
	Real3 Center() const { return point + (0.5f * extent); }

	// Moves the bbox, so that the center is at (0,0,0), returns distance moved
	Real3 Centralize();

	// More on Boundries
	float X() { return extent.x; }
	float Y() { return extent.y; }
	float Z() { return extent.z; }

	// Calculates Points for a vertex buffer
	void CalculateVertexPoints(vector<Real3> &ptr, const Real3 &pos);

	// Roates the box
	//void Rotate();

	// Clears out the box
	void Clear();
	
	// Check for overlap
	friend bool BBoxCollides(const BBox &a, const BBox &b);
	friend bool BBoxCollides(const BBox &a, const D3DXMATRIX *aRot, const BBox &b, const D3DXMATRIX *bRot);

private:

	// Min and max values for the box
	Real3 point;
	Vector3 extent;

	friend class Octree;
};

#endif // __BBOX_H__